/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import it.polimi.se2.meteocal.control.CalendarManager;
import it.polimi.se2.meteocal.control.EventManager;
import it.polimi.se2.meteocal.control.LocationManager;
import it.polimi.se2.meteocal.control.UserManager;
import it.polimi.se2.meteocal.entity.Event;
import it.polimi.se2.meteocal.entity.Location;
import it.polimi.se2.meteocal.entity.Users;
import java.sql.Time;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 *
 * @author Ugo
 */
@RunWith(Arquillian.class)
public class CalendarManagerIT {
    
    public CalendarManagerIT() {
    }
    
    @EJB
    CalendarManager calendarManager;
    @EJB
    EventManager eventManager;
    @EJB
    UserManager userManager;
    @EJB
    LocationManager locationManager;
   
    @PersistenceContext
    EntityManager entityManager;
   
    private final String email = "test@test.it";
   
    @Deployment
    public static WebArchive createArchiveAndDeploy() {
        return ShrinkWrap.create(WebArchive.class)
                .addClass(CalendarManager.class)
                .addClass(EventManager.class)
                .addClass(UserManager.class)
                .addClass(LocationManager.class)
                .addPackage(Users.class.getPackage())
                .addPackage(Event.class.getPackage())
                .addPackage(Location.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }
   
   
    @Test
    public void CalendarManagerShouldBeInjected() {
        assertNotNull(calendarManager);
    } 
    
    @Test
    public void UserManagerShouldBeInjected() {
        assertNotNull(userManager);
    }
    
    @Test
    public void EventManagerShouldBeInjected() {
        assertNotNull(eventManager);
    }
   
    @Test
    public void EntityManagerShouldBeInjected() {
        assertNotNull(entityManager);
    }
    
    @Test
    @InSequence(1)
    public void creationCalendar(){
        Users newUser = new Users();
        String password = "password";
        newUser.setEmail(email);
        newUser.setName("Ugo");
        newUser.setSurname("De Vos");
        newUser.setPassword(password);
        newUser.setCity("city");
        userManager.save(newUser);
        
        assertNotNull(calendarManager.findCalendarByUser(newUser));
        assertFalse(calendarManager.findCalendarByUser(newUser).isPrivate());
    }
    
    @Test
    @InSequence(2)
    public void addNewEvent(){
        Users user = userManager.findUserByEmail(email);
        Event newEvent = new Event();
        newEvent.setCalendar(calendarManager.findCalendarByUser(user));
        newEvent.setTitle("Title");
        newEvent.setData(new Date());
        newEvent.setBeginn(new Time(12, 0, 0));
        newEvent.setEndd(new Time(20, 0, 0));
        locationManager.create(new Location("Milan"));
        newEvent.setLocation(new Location("Milan"));
        newEvent.setPrivatee(false); //!!!!!!!!!!!!!!!
        eventManager.create(newEvent);
        
        List<Event> events;
        events=calendarManager.findEventsInCalendar(calendarManager.findCalendarByUser(user));
        
        assertTrue(newEvent.getId().equals(events.get(0).getId()));
        assertTrue(events.contains(newEvent));
        assertFalse(events.get(0).isPrivatee());
        assertEquals(1, events.size());
        assertTrue(events.get(0).getCalendar().equals(calendarManager.findCalendarByUser(user)));
    }
    
    @Test
    @InSequence(3)
    public void checkPrivateEventProperty(){
        Users user = userManager.findUserByEmail(email);
        Event newEvent = new Event();
        newEvent.setCalendar(calendarManager.findCalendarByUser(user));
        newEvent.setTitle("Title");
        newEvent.setData(new Date());
        newEvent.setBeginn(new Time(12, 0, 0));
        newEvent.setEndd(new Time(20, 0, 0));
        locationManager.create(new Location("Rome"));
        newEvent.setLocation(new Location("Rome"));
        newEvent.setPrivatee(true); //!!!!!!!!!!!!!!!
        eventManager.create(newEvent);
        List<Event> events=calendarManager.findEventsInCalendar(calendarManager.findCalendarByUser(user));
        List<Event> eventspubb = calendarManager.findPublicEventsInCalendar(calendarManager.findCalendarByUser(user));
      
        assertTrue(events.contains(newEvent));
        assertFalse(eventspubb.contains(newEvent));
        
    }
    
   
}
